/**
 * 退出登录
 */
function logOut(sePath) {
	window.location.href = "/" + sePath + "/logout.htm";
}

function modifyMerchantOpPassword() {
	var dialogDom = $("<div id='modifyPasswordDivDialog'></div>");
	var loadUrl = "/local/dialog/operator.html #modifyPasswordDiv";
	dialogDom.load(loadUrl, function() {
		var form = $(this).find("form");
		$.parser.parse(form);
		var operator = currentOperator();
		$("#account").html(operator.account);
		$("#modiry_name").val(operator.name);
		var cancelBtn = $(this).find("#cancelBtn");
		cancelBtn.click(function() {
			dialogDom.dialog("destroy");
		});
		var saveBtn = $(this).find("#saveBtn");
		saveBtn.click(function() {
			form.attr("action", "/merse/modifyPassword.htm");
			form.form("submit", {
				success : function(text) {
					var data = parserToJson(text);
					if (data.success) {
						dialogDom.dialog("destroy");
					} else {
						$("#errorMsg").html(data.msg);
					}
				}
			});
		});
	});
	dialogDom.dialog({
		title : '修改密码',
		width : 450,
		modal : true,
		shadow : false,
		closed : false,
		height : 350,
		onClose : function() {
			$(this).dialog("destroy");
		}
	});
}

function searchOperator() {
	$('#operatorTable')
			.datagrid(
					{
						nowrap : true,
						striped : true,
						scrollbarSize : 0,
						fitColumns : true,
						// toolbar:"#toolbar",
						url : '/merse/operator/search.htm',
						method : 'get',
						pageSize : 10,
						pageNumber : 1,
						pagination : true,
						pageList : [ 10, 20 ],
						singleSelect : true,
						idField : 'id',

						columns : [ [
								{
									field : 'account',
									title : '帐号',
									width : $(this).width() * 0.1
								},
								{
									field : 'name',
									title : '姓名',
									width : $(this).width() * 0.1
								},
								{
									field : 'status',
									title : '状态',
									width : $(this).width() * 0.05,
									formatter : function(status) {
										return status.desc;
									}
								},
								{
									field : 'operatorRoles',
									title : '系统管理角色',
									width : $(this).width() * 0.4,
									formatter : function(operatorRoles) {
										var value = '';
										if (!operatorRoles)
											return value;
										for (var i = 0; i < operatorRoles.length; i++) {
											value += operatorRoles[i].role.name;
											if (i < operatorRoles.length - 1)
												value += ',';
										}
										return value;
									}
								},

								{
									field : 'createDate',
									title : '创建日期',
									width : $(this).width() * 0.1,
									formatter : function(date) {

										return formatDate(date, 'yyyy-MM-dd');
									}
								},
								{
									field : 'lastLoginDate',
									title : '最后登录日期',
									width : $(this).width() * 0.15,
									formatter : function(date) {
										return date ? formatDate(date,
												'yyyy-MM-dd hh:mm') : "";
									}
								},
								{
									field : 'handle',
									title : '操作',
									width : $(this).width() * 0.2,
									formatter : function(value, row, index) {
										var html = "<a class=\"fontGreen pd15 popBtn\" href=\"javascript:void(0);\" onclick=\"resetPassword("
												+ index + ")\">重置密码</a>";
										html += "<a class=\"pd15\" href=\"javascript:void(0);\" onclick=\"editOperator("
												+ index + ")\">修改</a>";
										if (row.status.value == 1) {
											html += "<a class=\"pd15\" href=\"javascript:void(0);\" onclick=\"freezeOperator("
													+ index + ")\">冻结</a>";
										} else {
											html += "<a class=\"pd15\" href=\"javascript:void(0);\" onclick=\"unFreezeOperator("
													+ index + ")\">解冻</a>";
										}
										return html;
									}
								} ] ],
						loadFilter : function(json) {
							var data = {};
							data.rows = json.operators;

							data.total = json.pagination.count;
							data.pagination = json.pagination;
							return data;
						},
						onAfterSelectPage : function() {
							var pageOpt = $('#operatorTable').datagrid(
									'getPager').pagination("options");

							var name = $("#opName").attr("value");
							var account = $("#opAccount").attr("value");
							$(this).datagrid("options").queryParams = {
								"ts" : new Date().getTime(),
								"currentPage" : pageOpt.pageNumber,
								"pageSize" : pageOpt.pageSize,
								"name" : name,
								"account" : account
							};
						}
					});
}

/**
 * 添加管理员
 */
function addOperator() {

	var dialogDom = $("<div id='addOperatorDiv'></div>");
	dialogDom.load("/local/dialog/operator.html #addOperatorDialog",
			function() {
				var form = $(this).find("form");
				$.parser.parse(form);

				$("#roles").combobox({
					url : '/data/json/roles.htm',
					method : 'get',
					valueField : 'id',
					textField : 'name',
					multiple : true,
					editable : false,
					panelHeight : "auto"
				});
				var cancelBtn = $(this).find("#cancelBtn");
				cancelBtn.click(function() {
					dialogDom.dialog("destroy");
				});
				var saveBtn = $(this).find("#saveBtn");
				saveBtn.click(function() {
					form.form("submit", {
						success : function(text) {
							var data = parserToJson(text);
							if (data.success) {
								dialogDom.dialog("destroy");
								$('#operatorTable').datagrid("reload");
							} else {
								$("#errorMsg").html(data.msg);
							}
						}
					});
				});
			});
	dialogDom.dialog({
		title : '添加管理员',
		width : 450,
		modal : true,
		shadow : false,
		closed : false,
		draggable : false,
		height : 400,
		onClose : function() {
			$(this).dialog("destroy");
		}
	});
}

/**
 * 编辑管理员
 */
function editOperator(index) {
	var row = ($('#operatorTable').datagrid('getRows'))[index];
	if (!row) {
		$.messager.alert('警告', '请选择一个账户');
		return;
	}
	if (isSelf(row)) {
		return;
	}
	var dialogDom = $("<div id='editOperatorDialog'></div>");
	var loadUrl = "/local/dialog/operator.html #editOperatorDiv";
	dialogDom.load(loadUrl, function() {
		var form = $(this).find("form");
		$.parser.parse(form);
		var operator = getOperatorData(row.id);
		fillEditOperatorData(operator);
		var cancelBtn = $(this).find("#cancelBtn");
		cancelBtn.click(function() {
			dialogDom.dialog("destroy");
		});
		var saveBtn = $(this).find("#saveBtn");
		saveBtn.click(function() {
			form.form("submit", {
				url : "/merse/operator/" + row.id + "/",
				success : function(text) {
					var data = parserToJson(text);
					if (data.success) {
						dialogDom.dialog("destroy");
						$('#operatorTable').datagrid("reload");
					} else {
						$("#errorMsg").html(data.msg);
					}
				}
			});
		});
	});
	dialogDom.dialog({
		title : '修改账户',
		width : 450,
		modal : true,
		shadow : false,
		closed : false,
		draggable : false,
		height : 250,
		onClose : function() {
			$(this).dialog("destroy");
		}
	});

	function fillEditOperatorData(operator) {
		$("#name").val(operator.name);
		$("#roles").combobox({
			url : '/data/json/roles.htm',
			method : 'get',
			valueField : 'id',
			textField : 'name',
			multiple : true,
			editable : false,
			loadFilter : function(roles) {
				for (var i = 0; i < roles.length; i++) {
					for (var j = 0; j < operator.operatorRoles.length; j++) {
						if (roles[i].id == operator.operatorRoles[j].role.id) {
							roles[i].selected = true;
							break;
						}
					}
				}
				return roles;
			}
		});

	}

	/**
	 * 得到操作员的数据
	 * 
	 * @param operatorId
	 */
	function getOperatorData(operatorId) {
		var url = "/merse/operator/" + operatorId + "/";
		var data = $.ajax({
			url : url,
			async : false
		}).responseText;
		var operator = parserToJson(data);

		return operator;
	}
}

/**
 * 判断是否是自己
 */
function isSelf(row) {
	var operator = currentOperator();
	if (row.id == operator.id) {
		$.messager.alert('警告', '不能操作自己');
		return true;
	}
	return false;
}
/**
 * 冻结管理员
 */
function freezeOperator(index) {
	var row = ($('#operatorTable').datagrid('getRows'))[index];
	if (!row) {
		$.messager.alert('警告', '请选择一个管理员');
		return;
	}
	if (isSelf(row)) {
		$.messager.alert('警告', '你不能冻结自己');
		return;
	}
	if (row.status.value == 0) {
		$.messager.alert('警告', '该账号已被冻结');
		return;
	}
	$.messager.confirm('冻结管理员', '你确定要冻结 ' + row.name + ' ?', function(ok) {
		if (ok) {
			var url = "/merse/operator/" + row.id + "/freeze.htm";
			$.ajax({
				url : url,
				async : false,
				type : 'post',
				data : {
					'_method' : 'put'
				},
				dataType : 'json',
				success : function(data) {
					if (data.success) {
						$('#operatorTable').datagrid("reload");
					} else {
						$.messager.alert('警告', data.msg);
					}

				}
			});
		}
	});

}

/**
 * 解冻管理员
 */
function unFreezeOperator(index) {
	var row = ($('#operatorTable').datagrid('getRows'))[index];
	if (!row) {
		$.messager.alert('警告', '请选择一个管理员');
		return;
	}
	if (isSelf(row)) {
		return;
	}
	if (row.status.value == 1) {
		$.messager.alert('警告', '该管理员是有效状态');
		return;
	}
	$.messager.confirm('解冻管理员', '你确定要解冻 ' + row.name + ' ?', function(ok) {
		if (ok) {
			var url = "/merse/operator/" + row.id + "/unfreeze.htm";
			$.ajax({
				url : url,
				async : false,
				type : 'post',
				data : {
					'_method' : 'put'
				},
				dataType : 'json',
				success : function(data) {
					if (data.success) {
						$('#operatorTable').datagrid("reload");
					} else {
						$.messager.alert('警告', data.msg);
					}
				}
			});
		}
	});
}

/**
 * 重置密码
 */
function resetPassword(index) {
	var row = ($('#operatorTable').datagrid('getRows'))[index];
	if (!row) {
		$.messager.alert('警告', '请选择一个管理员');
		return;
	}
	$.messager.confirm('重置密码', '你确定要重置 ' + row.name + '密码 为:111111?', function(
			ok) {
		if (ok) {
			var url = "/merse/operator/" + row.id + "/resetPassword.htm";
			$.ajax({
				url : url,
				async : false,
				type : 'post',
				data : {
					'_method' : 'put'
				},
				dataType : 'json',
				success : function(data) {
					if (data.success) {
						$.messager.alert('提示', '重置密码成功!');
						$('#operatorTable').datagrid("reload");
					} else {
						$.messager.alert('警告', data.msg);
					}
				}
			});
		}
	});
}